/*
 * MediaPlayer.cpp
 *
 *  Created on: Mar 11, 2011
 *      Author: ToanNhan
 */

#include "../inc/MediaPlayer.h"

using namespace Osp::Base;
using namespace Osp::Media;
using namespace Osp::Graphics;
using namespace Osp::Ui::Controls;

Player* pPlayer = null;
MediaPlayerListener* pListener = null;
OverlayPanel * pPlayerPanel = null;
BufferInfo bufferInfoforVideo;

// Step 1: Implement IPlayerEventListener.
MediaPlayerListener::MediaPlayerListener(void)
{
}

void
MediaPlayerListener::OnPlayerOpened(result r)
{  // Step 6: Play the media
   pPlayer->Play();
}

void
MediaPlayerListener::OnPlayerEndOfClip(void)
{  // Step10: Close the file
   pPlayer->Close();

   // Step11: delete all create object
   delete pPlayer;
   delete this;
}

void
MediaPlayerListener::OnPlayerBuffering(int percent)
{
}

void
MediaPlayerListener::OnPlayerErrorOccurred(PlayerErrorReason r)
{
}

void
MediaPlayerListener::OnPlayerInterrupted(void)
{
}

void
MediaPlayerListener::OnPlayerReleased(void)
{
}

MediaPlayer::MediaPlayer()
{
}

MediaPlayer::~MediaPlayer()
{

}

result MediaPlayer::PlayMusic(String fileName)
{
	result r = E_SUCCESS;

	// Step 2: Create the listener
	pListener = new MediaPlayerListener;


	// Step 4: Create Player using the listener    and the Canvas
	pPlayer = new Player();
	r = pPlayer->Construct(*pListener, &bufferInfoforVideo);
	if (IsFailed(r))
	{
		goto CATCH;
	}

	// Step 5: Open a video file asynchnously
	r = pPlayer->OpenFile(fileName, true);
	if (IsFailed(r))
	{
		goto CATCH;
	}

CATCH:
	return r;

}

result MediaPlayer::StopMusic()
{
	result r = E_SUCCESS;

	// Step 9: Stop the playing.
	pPlayer->Stop();
	return r;
}
